import {createRouter, createWebHistory} from "vue-router"

import HomeView from "../components/home.vue"
// import AboutView from "../views/AboutView.vue"


const routes = [
    // path 必填字段，component 必填字段，name 非必填字段，路由名字，建议加上
    // 通过懒加载方式注册组件
    {path: "/login", component: () => import("../components/login/login.vue"), name: "login"},
    { path: '/', redirect: '/index' },
    {
        name: 'index', 
        path: '/index', 
        component: () => import("../components/index.vue"), 
        redirect: '/index/home', 
        children: [
          { name: 'home', path: 'home', component: HomeView },
          //   rdp管理1
          // {
          //   name: 'rdp', path: '/rdp', component: () => import("../components/rdp/rdp.vue"), 
          // },
          //   gitlabManage
          {
            name: 'gitlabManage', path: '/gitlabManage', component: () => import("../components/gitlabManage/index.vue"), 
            redirect: '/gitlabManage/tags', children: [
              { name: 'tags', path: 'tags', component: () => import("../components/gitlabManage/tags.vue") },
              { name: 'filediff', path: 'filediff', component: () => import("../components/gitlabManage/fileDiff.vue") },
              { name: 'filediffUp', path: 'filediffUp', component: () => import("../components/gitlabManage/fileDiffUpload.vue") },
            ]
          },
          //   rdp管理3
          // {
          //   name: 'rdp3index', path: '/rdp3', component: () => import("../components/gitlabManage/index.vue"), 
          //   children: [
          //     { name: 'rdp3', path: 'rdp3', component: () => import("../components/gitlabManage/tags.vue") },
          //   ]
          // },
        //   // 菜单管理
        //   {
        //     name: 'menuindex', path: '/menu', component: menuindex, redirect: '/menu/menuget', children: [
        //       { name: 'menuget', path: 'menuget', component: menuget },
        //     ]
        //   },
        ]
      },
    // {
    //     path: "/about", component: () => import('../views/AboutView.vue'), name: "about", redirect: "/about/aboutus",
    //     children: [
    //         // 注意：二级路径 path 不要加 /
    //         {path: "aboutus", name: "aboutus", component: () => import("../views/AboutSub/AboutUS.vue")},
    //         {path: "aboutinfo", name: "aboutinfo", component: () => import("../views/AboutSub/AboutInfo.vue")},
    //     ]
    // },
]


const router = createRouter({
    // createWebHashHistory    这种路径里面会有井号 # 出现，
    // createWebHistory        此种方式需要后台做重定向，否则会出现404
    history: createWebHistory(),
    routes
})

// 全局路由导航守卫
router.beforeEach((to, from, next) => {
    // 判断是否为登录页面，是则放行
    // console.log('xxxxxxxx', to.path);
    // console.log('bbbbbbbb')
    if (to.path === '/login' || to.path === '/jwt') return next()
    // 获取 token 值
    const auth = localStorage.getItem('auth')
    // console.log("是否有token:", token);
    // 如果 token 不存在，跳转到登录页面
    if (!auth) {
      return next('/login')
      // console.log('xxxxxxxx222', to.path);
    }
    // token 存在，直接放行
    next()
})

export default router;